package com.bbmall.dwd;

import com.bbmall.SparkSessionUtil;
import org.apache.spark.sql.SparkSession;

/**
 * 会员维度表抽取到DWD层
 */
public class OdsToDwdDimMember {
    public static void main(String[] args) {
        SparkSession spark = SparkSessionUtil.getSparkSession("ODS_TO_DWD_DIM_MEMBER");

        try {
            String sql = "INSERT OVERWRITE TABLE dwd.dwd_dim_member PARTITION (dt) " +
                    "SELECT " +
                    "  member_id,              -- 会员ID" +
                    "  member_name,            -- 会员姓名" +
                    "  phone_number,           -- 手机号" +
                    "  gender,                 -- 性别" +
                    "  birthday,               -- 生日" +
                    "  -- 计算年龄：当前年份 - 生日年份，生日为空则设为0" +
                    "  CASE WHEN birthday IS NOT NULL THEN " +
                    "    YEAR(CURRENT_DATE()) - YEAR(TO_DATE(birthday)) " +
                    "  ELSE 0 END AS age, " +
                    "  register_date,          -- 注册日期" +
                    "  member_level,           -- 会员等级" +
                    "  total_consumption,      -- 累计消费金额" +
                    "  last_consumption_date,  -- 最后消费日期" +
                    "  status,                 -- 状态" +
                    "  created_time,           -- 创建时间" +
                    "  updated_time,           -- 更新时间" +
                    "  dt                      -- 分区字段" +
                    "FROM ods.ods_member_info " +
                    "WHERE dt = (SELECT MAX(dt) FROM ods.ods_member_info);";

            spark.sql(sql);
            System.out.println("会员维度表（dwd_dim_member）数据抽取完成！");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (spark != null) spark.stop();
        }
    }
}